package br.com.alinesoftware.app.controller.converter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.apache.commons.lang.StringUtils;

public class CEPConverter implements Converter {

	public Object getAsObject(FacesContext contexto, UIComponent arg1, String cgc) {
		if (StringUtils.isNotEmpty(cgc)) {
			Pattern p = Pattern.compile("(\\d\\d).(\\d\\d\\d)-(\\d\\d)");
			Matcher m = p.matcher(cgc);
			return Long.parseLong(m.replaceAll("$1$2$3"));
		}
		return null;
	}

	public String getAsString(FacesContext contexto, UIComponent arg1, Object cgc) {
		if (cgc != null) {
			Pattern p = Pattern.compile("(\\d\\d)(\\d\\d\\d)(\\d\\d)");
			Matcher m = p.matcher(String.valueOf(cgc));
			return m.replaceAll("$1.$2-$3");
		}
		return null;
	}

}
